Xi-Language Reference: Statements

    • Basic Statements
    • Blocks or Lists of Statements
    • Declarations
    • Function Definitions
    • Conditionals
    • Loops

    Basic Statements

    One of the basic statements is the empty statement which only consists of a single semicolon:
                     ;
    
    As the name implies the empty statement really does nothing it has only compatiblity meaning (it should make no difference how many semicolons come after a valid statement).

    Okay, now here come the statements that are of real use. First of all a statement may by an assignement expression, a function call expression or a IO operation expression:

                     <expr> ;
    
    Keep in mind that at this point <expr> does not mean any type of expression, e.g. 5+4; is not a valid statement like a=5+4;. The expression <expr> will be calculated and the result goes to nowhere.

    In the following a valid stetement may be shorten by <stmt>

    Blocks or Lists of Statements

    A block is a list of statements encapsulated in '{' and '}':
                     { <stmt1> ; <stmt2> ;... }
    
    A block is a statement by itself it simply executes <stmt1> then <stmt2> and so on. You can use blocks, if you want to delay the execution of several statements, the commandline will wait until you have completed the block before it executes the statements inside. Usually you need blocks together with loops and conditionals.

    In the following a valid block may be shorten by <block>

    Declarations

    Next comes the declaration statement we've already talked about. Let's see what you've learned ;-): If you want a variable that type is fixed (e.g. you want a variable to be an integer an stay an integer whatever comes next) you have to make a declaration statement. A declaraion statement may be one of the following:
                    <type> <id1> , <id2> ,... ;
                    <type> <id1> [ ] , <id2> [ ] ,... ;
                    <type> <id> [ <num1> , <num2> ,... ] ;
                    <type> <id> [ <num1> ] [ <num2> ]... ;
    
    Where <type> is one of the following: char, short, int, float, double, complex, string. Each type (execpt complex and string) is the Xi representation of the corresponding C-type, they should not be explained here. The type complex is simply a complex number which internally is two numbers of type double (the real and imaginary part). With a complex number you can do most of the things you can do with a double number. Because Xi does not understand pointers the string type is the equivalent to char * in C, except that you don'f have to bother with the memory allocation.

    If you type a [ ] or [ <num1> , <num2> ,... ] or [ <num1> ] [ <num2> ]... after the variable name (<id>) Xi will generate an array of <type>. If you only use [] the array is of undefined dimension and the array consists of only one number. Alternatively you can explicitly set the dimension of the array (e.g. double a[10,10] will generate a 10x10 Matrix of double numbers).

    Function definition

    In a previous section Function Calls were explaeined but the question how the user may implement his own functions in Xi has been left open. Basically a function call is one of the following:
                    <rettype> <id_f> ( <id1> , <id2> ,...) <block>
                    <rettype> <id_f> ( <type> <id1> , <type> <id2> ,...) <block>
                    <rettype> <id_f> ( <type> <id1> [ ] , <type> <id2> [ ] ,... ) <block>
    
    Where <rettype> indicates the type of the return value of the function <id_f>and may be one of the following: void (any type), char, short, int, float, double, complex, string, char[], short[], int[], float[], double[], complex[] (the latter indicating that the return value is an array). <id1>, <id2>,... are the parameters of the function. If no type is given the parameters are of type Any if this is not the case there will be a precise typechecking (alternatively you can think of a declaration). The '[]' after the <idn> indicates an array with arbitrary dimension (if you want to check the dimensions of the parameters you should use the size function). Note that it is necessary to specify a return type, but not the type of the parameters.

    Of course a function can have a return value. For this purpose Xi has the return statement:

                    return ;
                    return <expr> ;
    
    In the first case the function only returns (i.e. the statements after return will not be executed) in the second case the result of <expr> will be returned by the function. The result of <expr> might has to be cast to the given return type <rettype>, think of a build in cast expression (see Calculation Expression for detail).

    Sometimes a function may have more than one return value. Since Xi supports no pointers it is necessary to define a special function definition:

                    [ <rettype1> , <rettype2> ,... ] <id_f> ( <params> ) <block>
    
    Here <params> should indicate the parameters as above. <rettype1>,<rettype2>,... are the types of the return values. To return more than one value an extended return statement is necessary:
                    return [ <expr1> , <expr2> ,... ] ;
    
    With this statement the function returns the values of <expr1>, <expr2>,... with the given types <rettype1>, <rettype2>,...

    Conditionals

    The Comparison Expressions are usesless if there is no expression to conditionaly branch the path of execution. That is what the if statement is for:
                    if ( <expr> ) <then-stmt>
                    if ( <expr> ) <then-stmt> else <else-stmt>
    
    Usually <expr> is a comparison expression, the if statement tests whether the result of <expr> is true (i.e. it is not zero) It then executes <then-stmt>. If <expr> is false the if statement executes <else-stmt> when given.

    Loops

    If you want a block of statements to be executed more than once you are in need of a loop. Xi supports two type of loops: The for statement and the while statement. First the for statement:
                    for ( <begin-expr> ; <cond-expr> ; <after-expr> ) <stmt>
    
    The for statement first calculates <begin-expr> (usually this is an assignment that sets the start value of a counter). It then caluculates <cond-expr> and tests if the result is true (usually this is an abort condition for the counter). If so <stmt> will be executed and after this <after-expr> will be calculated (usually this is the increment of the counter). Now <cond-expr> will be tested again and if true stmt executed a second time and so on. The exection ends when the result of <cond-expr> is false.

    The while-stmt is a little easier:

                    while ( <cond-expr> ) <stmt>
    
    The while statement simply executes <stmt> as long as the result of <cond-expt> is true.

    The break statement causes the innermost enclosing loop to be left immediately. The continue statement causes the next iteration of the enclosing loop to begin.


    © 1995 by Bodo Junglas, Klaus Spanderen and Fabian Weis
    - Last revised: May 30 1996